extends Node

var _screenshot = null
var _second = 0
var _onceInit = false

func _ready():
	pass
	
func _process(delta):
	if !_onceInit:
		_second += delta
		if _second >= 0.5:
			_firstInit()

func _firstInit():
	loadCommonData()
	_onceInit = true

func toStory():
	Model.title.visible = false
	pass

func toLogo():
	Model.logo.fadein()
	Model.play.clearStory()
	Model.title.visible = true
	Audio.stop()
	pass
	
func showCgLibrary():
	Model.library.setCgMode()
	Model.library.visible = true
	pass
	
func showMusicLibrary():
	Model.library.setMusicMode()
	Model.library.visible = true
	pass
	
enum DataManagerMode {
	LOAD,
	SAVE
}

func catchScreen():
	_screenshot = Model.game.get_viewport().get_texture().get_data()
	_screenshot.flip_y()

func showDataManager(mode, onlyLoad : bool):
	if mode == DataManagerMode.SAVE:
		Model.save.setSaveMode()
	else:
		Model.save.setLoadMode()
		if onlyLoad:
			Model.save.setOnlyLoad()
	Model.save.visible = true
	catchScreen()
	pass

func showOptions():
	Model.options.checkOption()
	Model.options.visible = true
	pass

func showHistory():
	Model.history.visible = true
	pass
	
func saveGame(number : int):
	var save_pass = Config.save_password
	var dir = Directory.new()
	var save_path = "user://save_data_{number}".format({
		"number": number
	})
	if !dir.dir_exists(save_path):
		dir.make_dir(save_path)
	#print(OS.get_user_data_dir())
	var save_game = File.new()
	var err = save_game.open_encrypted_with_pass(save_path + '/' + "savedata.sav", File.WRITE, save_pass)
	if err:
		print('savegame error: ', err)
		return
	_onSave(save_game)
	save_game.close()
	
	# 截屏
	_screenshot.save_png(save_path + '/' + 'screenshot.png')
	
func loadGame(number : int):
	var save_pass = Config.save_password
	var save_path = "user://save_data_{number}".format({
		"number": number
	})
	var file_name = save_path + '/' + "savedata.sav"
	var save_game = File.new()
	if not save_game.file_exists(file_name):
		print('存档文件[{number}]不存在!'.format({
			"number": number
		}))
		return
	var err = save_game.open_encrypted_with_pass(file_name, File.READ, save_pass)
	if err:
		print('loadgame error: ', err)
		return
	_onLoad(save_game)
	save_game.close()
	
func _onSave(f : File):
	f.store_line(to_json({
		"save_version": "1.0.0",
		"save_flag": "godot-lite2d",
		"game_name": Config.game_name,
		"game_chapter": Model.chapter,
		"play_time": Model.play_time,
		"save_time": OS.get_unix_time(),
	}))
	f.store_line(to_json(Model.play.layer.getSaveData()))
	f.store_line(to_json(Model.dialog.getSaveData()))
	f.store_line(to_json(Model.select.getSaveData()))
	f.store_line(to_json(Model.play.getSaveData()))
	f.store_line(to_json(Model.game_values))
	print('savegame successed.')
		
func _onLoad(f : File):
	
	# 游戏信息加载
	var info = parse_json(f.get_line())
	Model.play_time = info.play_time
	Model.chapter = info.game_chapter
	
	# 场景精灵加载
	var layerInfo = parse_json(f.get_line())
	Model.play.layer.setSaveData(layerInfo)
	
	# 对话框加载
	var dialogInfo = parse_json(f.get_line())
	Model.dialog.setSaveData(dialogInfo)
	
	# 分支选择加载
	var selectInfo = parse_json(f.get_line())
	Model.select.setSaveData(selectInfo)
	
	# 故事脚本加载
	var storyInfo = parse_json(f.get_line())
	Model.play.setSaveData(storyInfo)
	
	# 游戏变量加载
	Model.game_values = parse_json(f.get_line())
	
	print('loadgame successed.')
	

func saveCommonData():
	var save_pass = Config.save_password
	var file_name = "user://gamecommon.sav"
	var save_game = File.new()
	var err = save_game.open_encrypted_with_pass(file_name, File.WRITE, save_pass)
	if err:
		print('savecommon error: ', err)
		return
	var f = save_game
	f.store_line(to_json(Model.options.getSaveData()))
	f.store_line(to_json(Model.game_common_values))
	f.close()
	
func loadCommonData():
	var save_pass = Config.save_password
	var file_name = "user://gamecommon.sav"
	var save_game = File.new()
	if not save_game.file_exists(file_name):
		print('文件[gamecommon.sav]不存在!')
		return
	var err = save_game.open_encrypted_with_pass(file_name, File.READ, save_pass)
	if err:
		print('loadcommon error: ', err)
		return
	var f = save_game
	Model.options.setSaveData(parse_json(f.get_line()))
	Model.game_common_values = parse_json(f.get_line())
	f.close()
